#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>

/**
 * 数列NOIP2006
 * 
 */
int main() {
	std::string binaryNum;
	long long result = 0;

	int k, N;
	std::cin >> k >> N;

	while (N) {
		binaryNum += std::to_string(N % 2);
		N /= 2;
	}

	std::reverse(binaryNum.begin(), binaryNum.end());

	int weight = 0;
	for (int i = binaryNum.length() - 1; i >= 0; i--) {
		result += (binaryNum[i] - '0') * (long long)std::pow(k, weight++);
	}

	std::cout << result;

	return 0;
}
